package com.cho3.module;

import android.app.Activity;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.cho3.adapter.MatchesAdapter;
import com.cho3.dialog.MatchesDialog;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class VpModule extends ReactContextBaseJavaModule {
    private WritableMap mResponse;
    private Callback mCallBack;
    private MatchesDialog dialog;
    private Activity currentActivity;

    public VpModule(ReactApplicationContext reactContext) {
        super(reactContext);
        mResponse = Arguments.createMap();
    }

    @Override
    public String getName() {
        return "vp";
    }

    @ReactMethod
    public void showDialog() {
        currentActivity = getCurrentActivity();
        if (currentActivity != null) {
            if (dialog == null) {
                dialog = new MatchesDialog(currentActivity);
            }
            dialog.show();
            dialog.getMatchesAdapter().setOnItemClickListener(position -> {
                mResponse=Arguments.createMap();
                LogUtils.iTag("666678", position);
                mResponse.putString("position", position + "");
                sendEventToJs("onItemClick", mResponse);
                mResponse = null;
                dialog.dismiss();
            });
        }
    }

    /**
     * @param eventName 事件的名称
     * @param
     */
    public void sendEventToJs(String eventName, WritableMap params) {
        getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
    }
}
